﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;
using System.Windows.Forms;

  /**
  * @file ChrisKit.cs
  * 
  * 從文豪的IOS_KIOSK_Webservice拷貝出來的工具
  * 
  * @date 2014/04/14
  * @version 1.0.0
  * @author Chris 
  * @note 相關注意事項可置於此。
  **/

  public enum QuotesType { SingleQ, DoubleQ };
  public enum HashType { MD5, SHA1, SHA256, SHA512 };

  public class ChrisKit
  {
    const string pattern1 = "-[A-Z]{2}[A-Z0-9][0-9]{3}";

    public static string trimCustomerName(string customerName) {
      string newCustName = customerName.Trim();
      
      newCustName = newCustName.Replace("股份有限公司", "股份");
      newCustName = newCustName.Replace("有限公司", "");
      newCustName = newCustName.Replace("化學工廠", "");

      newCustName = newCustName.Replace("(PVA)", "");
      newCustName = Regex.Replace(newCustName, pattern1, "");

      if (newCustName.Length >= 6)
        newCustName = newCustName.Replace("股份", "");
      if (newCustName.Length >= 6)
        newCustName = newCustName.Replace("化學工業", "化工");
      if (newCustName.Length >= 6)
        newCustName = newCustName.Replace("塑膠工業", "塑膠");
      if (newCustName.Length >= 6)
        newCustName = newCustName.Replace("分公司", "(分)");

      return newCustName;
    }

    /// <summary>
    /// Get Hash String
    /// </summary>
    /// <param name="str"></param>
    /// <param name="hashType">Hash function type</param>
    /// <returns></returns>
    public static string getHashStr(string str, HashType hashType) {
      HashAlgorithm hashObj;

      if (hashType == HashType.MD5)
        hashObj = new MD5CryptoServiceProvider();
      else if (hashType == HashType.SHA1)
        hashObj = new SHA1Managed();
      else if (hashType == HashType.SHA256)
        hashObj = new SHA256Managed();
      else if (hashType == HashType.SHA512)
        hashObj = new SHA512Managed();
      else
        return null;

      byte[] hashBytes = hashObj.ComputeHash(Encoding.UTF8.GetBytes(str));
      hashObj.Clear();

      string hashStr = "";
      for (int i = 0; i < hashBytes.Length; i++) {
        hashStr += hashBytes[i].ToString("x2");
      }
      return hashStr;
    }

    #region String Processing

    /// <summary>
    /// Get String with quotation marks
    /// </summary>
    /// <param name="str"></param>
    /// <param name="quotesType">quotation marks type</param>
    /// <returns></returns>
    public static string quoteStr(string str, QuotesType quotesType) {
      if (quotesType == QuotesType.SingleQ)
        return "'" + str + "'";
      else if (quotesType == QuotesType.DoubleQ)
        return "\"" + str + "\"";
      else
        return str;
    }

    public static string getDecimalCommaStr(object numObj) {
      decimal numValue;
      try {
        numValue = Convert.ToDecimal(numObj);
      }
      catch (Exception ex) {
        skipVar(ex);
        return null;
      }

      if (decimal.Truncate(numValue) == numValue) {// 整數
        if (numValue == 0)
          return "0";
        else
          return string.Format("###,###,###,###,###", numValue);
      }
      else {// 有小數位
        if (numValue >= 10000)
          return string.Format("###,###,###,###,###", numValue);
        else if (numValue < 1)
          return string.Format("0.####", numValue);
        else if (numValue < 1000)
          return string.Format("###.####", numValue);
        else // 10000 > number >= 1000
          return string.Format("##,###.##", numValue);
      }
    }

    public static string fixErrorChar(string inputStr) {
      string result = inputStr;
      result = result.Replace('', '伃');
      //result = result.Replace('', '喆');
      //result = result.Replace('', '堃');
      //result = result.Replace('', '峯');
      //result = result.Replace('', '媖');
      //result = result.Replace('', '瑨');
      //result = result.Replace('', '犇');
      //result = result.Replace('', '珏');
      //result = result.Replace('', '烱');
      return result;
    }

    #endregion

    #region Resource File Handle

    //public static System.IO.Stream getResourceStream(string fileName) {
    //  System.Reflection.Assembly asm =
    //    System.Reflection.Assembly.GetExecutingAssembly();
    //  string asmName = asm.GetName().Name;
    //  return asm.GetManifestResourceStream(asmName + "." + fileName);
    //}

    public static string getResourceText(string fileName) {

     
      System.IO.StreamReader reader =
        new System.IO.StreamReader(fileName);
      return reader.ReadToEnd();
    }

    #endregion

    #region Null or Exception Handle

    public static string emptyNull(string inputStr) {
      if (inputStr == null)
        return "";
      else
        return inputStr;
    }

    public static void skipVar(object obj) {
      // Use this function to skip the CS0168 warning.
      // For example:
      // "The variable 'ex' is declared but never used"
    }

    public static XmlNode stringToNode(string inputStr) {
      XmlDocument doc = new XmlDocument();
      XmlElement node = doc.CreateElement("OnlyString");
      node.InnerText = inputStr;
      return node;
    }

    #endregion

    public static byte[] getEncryptStr(string plaintext, string key, string iv) {
      RijndaelManaged rijndaelAlg = new RijndaelManaged();
      rijndaelAlg.Key = Encoding.ASCII.GetBytes(key);
      rijndaelAlg.IV = Encoding.ASCII.GetBytes(iv);
      ICryptoTransform encryptor = rijndaelAlg.CreateEncryptor();

      byte[] sourceBytes = Encoding.UTF8.GetBytes(plaintext);
      byte[] encryptedBytes = encryptor.TransformFinalBlock(sourceBytes, 0, sourceBytes.Length);
      return encryptedBytes;
    }
  }// End of class ChrisKit

